local yazi = fk.CreateSkill{
  name = "hanqing__yazi",
  tags = { Skill.Compulsory, Skill.Switch },
  dynamic_desc = function(self, player)
    if player:getMark("hanqing_ex__yazi") == 0 then
      return "hanqing__yazi1_"..player:getSwitchSkillState(self.name, false, true)
    else
      return "hanqing__yazi2_"..player:getSwitchSkillState(self.name, false, true)
    end
  end,
}

Fk:loadTranslationTable{
  ["hanqing__yazi"] = "睚眥",
  [":hanqing__yazi"] = "转换技，你可于相应时机发动〖恩怨〗并可：①弃置一张牌，将〖睚眥〗修改为锁定技；②回复1点体力。",

  [":hanqing__yazi1_yang"] = "转换技，你可以于相应时机发动〖恩怨〗，然后你可以"..
  "<font color=\"#E0DB2F\">①弃置一张牌，将〖睚眥〗改为锁定技；</font>"..
  "<font color=\"gray\">②回复1点体力。</font>",
  [":hanqing__yazi1_yin"] = "转换技，你可以于相应时机发动〖恩怨〗，然后你可以"..
  "<font color=\"#gray\">①弃置一张牌，将〖睚眥〗改为锁定技；</font>"..
  "<font color=\"#E0DB2F\">②回复1点体力。</font>",
  [":hanqing__yazi2_yang"] = "转换技，锁定技，你于相应时机发动〖恩怨〗，然后你"..
  "<font color=\"#E0DB2F\">①弃置一张牌；</font>"..
  "<font color=\"gray\">②回复1点体力。</font>",
  [":hanqing__yazi2_yin"] = "转换技，锁定技，你于相应时机发动〖恩怨〗，然后你"..
  "<font color=\"gray\">①弃置一张牌</font>；"..
  "<font color=\"#E0DB2F\">②回复1点体力。</font>",

  ["#hanqing__yazi-yang"] = "睚眥：是否弃置一张牌，将“睚眥”改为锁定技？",
  ["#hanqing__yazi-discard"] = "睚眥：请弃置一张牌",
  ["#hanqing__yazi-yin"] = "睚眥：是否回复1点体力？",


  ["$hanqing__yazi1"] = "非王稽，吾无以内函谷、居彻侯！",
  ["$hanqing__yazi2"] = "速持魏齐头来，不然，且屠大梁。",
}

local spec = {
  on_use = function (self, event, target, player, data)
    local room = player.room
    local skill = event == fk.AfterCardsMove and Fk.skills["hanqing__enyuan"] or Fk.skills["#hanqing__enyuan_2_trig"]
    skill:use(event, target, player, data)
    if player.dead then return end
    if player:getSwitchSkillState(yazi.name, true) == fk.SwitchYang then
      if not player:isNude() then
        if player:getMark("hanqing_ex__yazi") > 0 then
          room:askToDiscard(player, {
            min_num = 1,
            max_num = 1,
            include_equip = true,
            skill_name = yazi.name,
            cancelable = false,
            prompt = "#hanqing__yazi-discard",
          })
        elseif #room:askToDiscard(player, {
            min_num = 1,
            max_num = 1,
            include_equip = true,
            skill_name = yazi.name,
            cancelable = true,
            prompt = "#hanqing__yazi-yang",
          }) == 0 then
          return
        end
        if player:getMark("hanqing_ex__yazi") == 0 and not player.dead then
          room:setPlayerMark(player, "hanqing_ex__yazi", 1)
        end
      end
    elseif player:isWounded() then
      if player:getMark("hanqing_ex__yazi") > 0 or
        room:askToSkillInvoke(player, {
          skill_name = yazi.name,
          prompt = "#hanqing__yazi-yin",
        }) then
        room:recover{
          who = player,
          num = 1,
          recoverBy = player,
          skillName = yazi.name,
        }
      end
    end
  end,
}

yazi:addEffect(fk.AfterCardsMove, {
  anim_type = "switch",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(yazi.name) then
      local skill = Fk.skills["hanqing__enyuan"]
      return skill:triggerable(event, target, player, data)
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local targets = {}
    for _, move in ipairs(data) do
      if move.from and move.from ~= player and move.to == player and move.toArea == Card.PlayerHand and
        #move.moveInfo > 1 and not move.from.dead then
        table.insertIfNeed(targets, move.from)
      end
    end
    player.room:sortByAction(targets)
    for _, p in ipairs(targets) do
      if not player:hasSkill(yazi.name) then break end
      if not p.dead then
        event:setCostData(Fk.skills["hanqing__enyuan"], {tos = {p}})
        self:doCost(event, target, player, data)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player:getMark("hanqing_ex__yazi") == 0 then
      return Fk.skills["hanqing__enyuan"]:cost(event, target, player, data)
    else
      return true
    end
  end,
  on_use = spec.on_use,
})

yazi:addEffect(fk.Damaged, {
  anim_type = "switch",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(yazi.name) then
      local skill = Fk.skills["#hanqing__enyuan_2_trig"]
      return skill:triggerable(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player:getMark("hanqing_ex__yazi") == 0 then
      return Fk.skills["#hanqing__enyuan_2_trig"]:cost(event, target, player, data)
    else
      return true
    end
  end,
  on_use = spec.on_use,
})

return yazi
